package com.netbao.sys.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.netbao.model.PageBean;
import com.netbao.model.PageResult;
import com.netbao.model.Result;
import com.netbao.model.StatusCode;
import com.netbao.sys.mapper.RoleMapper;
import com.netbao.sys.model.Role;
import com.netbao.sys.model.RoleExample;
import com.netbao.sys.model.User;
import com.netbao.sys.service.RoleService;
import com.netbao.sys.utils.ShiroUtils;
import com.netbao.sys.utils.ValidatorUtil;
import com.netbao.sys.vomapper.RoleResVoMapper;
import com.netbao.sys.vomapper.RoleVoMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@Service
public class RoleServiceImpl implements RoleService {
    @Resource
    private RoleMapper roleMapper;
    @Override
    public PageResult list(PageBean pageBean, String roleName) {
        RoleExample roleExample=new RoleExample();
        roleExample.createCriteria()
                .andEidEqualTo(((User) ShiroUtils.getSessionAttribute(StatusCode.SESSIONUSER)).getEid())
                .andIsadminEqualTo(false)
                .andIsdelEqualTo(false);
        if(!StringUtils.isEmpty(roleName)){
            roleExample.createCriteria().andCnameLike("%"+roleName+"%");
        }
        PageHelper.startPage(pageBean.getPage(),pageBean.getRows());
        PageInfo<Role> rolePageInfo = new PageInfo<>(roleMapper.selectByExample(roleExample));
        return new PageResult(rolePageInfo.getTotal(),rolePageInfo.getList());
    }

    @Override
    public Result save(@Valid Role role) {
        ValidatorUtil.validatorEntity(role);
        role.setEid(((User) ShiroUtils.getSessionAttribute(StatusCode.SESSIONUSER)).getEid());
        if(role.getId()==null){
            roleMapper.insertSelective(role);
        }else {
            roleMapper.updateByPrimaryKeySelective(role);
        }
        return Result.ok();
    }

    @Override
    public Result delete(Long id) {
        Role role = new Role();
        role.setId(id);
        role.setIsdel(true);
        roleMapper.updateByPrimaryKeySelective(role);
        return Result.ok();
    }

    @Resource
    private RoleResVoMapper roleResVoMapper;
    @Override
    @Transactional
    public Result saveRes(Long roleid, String resids) {
        roleResVoMapper.delete(roleid);
        roleResVoMapper.insert(roleid,resids);
        return Result.ok();
    }


    @Transactional
    @Override
    public Result saveRes1(Map<String, Object> map) {
        roleResVoMapper.delete((Long) map.get("roleid"));
        roleMapper.saveres1(map);
        return Result.ok();
    }

    @Resource
    private RoleVoMapper roleVoMapper;
    @Override
    public List<Role> findByUserId(Long id) {
        List<Role> role = roleVoMapper.findByUserId(id);
        return role;
    }

    @Override
    public PageResult getRole(Long userid) {
        RoleExample roleExample=new RoleExample();
        roleExample.createCriteria()
                .andEidEqualTo(((User) ShiroUtils.getSessionAttribute(StatusCode.SESSIONUSER)).getEid())
                .andIsadminEqualTo(false)
                .andIsdelEqualTo(false);
        PageInfo<Role> rolePageInfo = new PageInfo<Role>(roleResVoMapper.getRole((long)userid));
        return new PageResult(rolePageInfo.getTotal(),rolePageInfo.getList());
    }
}
